<?php
namespace Framework\Library\Core;

/**
 * 实体类，一切有实质数据存在的对象的类型的基类
 */
abstract class Entity{
	
	public $module;
	protected $dbTable;
	public $childs;
	
	protected $db;
	protected $fs;
	
	public $data;
	
	function __construct($module,$table,$childs=NULL){
		
		$this->module = $module;
		$this->dbTable = $table;
		$this->childs = $childs;
		
		require_once 'Database.class.php';
		require_once 'FileSystem.class.php';
		
		$this ->db = new Database($module->application->config);
		$this ->fs = new FileSystem($module->application->config);
		
	}
	
	/**
	 * 从数据库加载数据，以激活对象，包括子对象
	 * @param unknown $id
	 * @return boolean
	 */
	public function activate( $id ){
		
		$data = $this->selectRecord($id);
		
		if (empty($data)) throw new \Exception('无效的对象标识');
		else $this->data = $data[0];
		
		if (is_array($this->childs)) foreach ($this->childs as $field=>$entity) $entity ->activate( $this->data[$field] );
		
	}
	
	public function newInstance( $id ){
		
		$class = get_class($this);
		$newInstance = new $class($this->module);
		$newInstance->activate( $id );
		
		return $newInstance;
		
	}
	
	/**
	 * 使用当前对象的DB_TABLE静态属性，从数据库读取指定记录
	 * @param int|array $id
	 * @return Ambigous <NULL, \Framework\Library\Core\multitype:, multitype:>|multitype:
	 */
	protected function selectRecord($id,$fields=NULL){
		
		return $this->db->select($this->dbTable,"`id` ".$this->db->makeIdCondition($id),NULL,NULL,$fields);
	
	}

	/**
	 * 插入一条或多条记录，成功时返回记录ID,失败时返回FALSE
	 * @param array $data 键名数组，对应数据库中的记录
	 * @return boolean|number
	 */
	protected function insertRecord( $data ){
		
		if (empty($data)) throw new \Exception('没有提供插入数据库的数据');
		
		$fields = array();
		$values = array();
		foreach ($data as $k=>$v){
			
			array_push($fields,"`$k`");
			array_push($values, "'".$this->db->escapeString($v)."'");
			
		}
		
		
		$sql = "INSERT INTO `".$this->dbTable."`(".implode(',',$fields).") ".
				"VALUES(".implode(',',$values).")";
	
		$state = $this->db->query($sql);
	
		if (!$state) return false;
		else return $this->db->insertId();
	
	}
	
	/**
	 * 更新一条或多条记录
	 * @param array $data
	 * @param int|array $id
	 * @return boolean
	 */
	protected function updateRecord( $data, $id=NULL ){
		
		if (empty($data)) throw new \Exception('没有提供更新数据库的数据');
		
		if (empty($id)) $id = $this->data['id'];
		
		$updates = array();
		foreach ($data as $k=>$v){
			
			array_push($updates, "`$k`='".$this->db->escapeString($v)."'");
				
		}
		
		
		$sql = "UPDATE `".$this->dbTable."` SET ".implode(',',$updates)." ".
				"WHERE `id` ".$this->db->makeIdCondition($id);
		
		$state = $this->db->query($sql);
		
		if (!$state) return false;
		else return true;
		
	}
	
	/**
	 * 删除一条或多条记录
	 * @param int|array $id
	 */
	protected function deleteRecord( $id ){
	
		$sql = "DELETE FROM `".$this->dbTable."` WHERE `id` ".$this->db->makeIdCondition($id);
		
		$state = $this->db->query($sql);
		
		$affectedRows = $this->db->affectedRows();
		
		if ($affectedRows > 0) return true;
		else return false;
	
	}
	
	public function dbTable(){
		return $this->dbTable;
	}
	
	public function getDb(){
		return $this->db;
	}
	
}